<?php

namespace Chaosyeo\SolarEngine;

class Properties implements Arrayable
{
    // 自定义属性集
    protected $customProperties = [];

    public function __construct(array $customProperties = [])
    {
        $this->setCustomProperties($customProperties);
    }
    
    public function setCustomProperties(array $customProperties): self
    {
        $this->customProperties = $customProperties;
        return $this;
    }

    public function setCustomProperty(string $field, string $value): self
    {
        $this->customProperties[$field] = $value;
        return $this;
    }

    public function toArray(): array
    {
        $data = [];
        $vars = get_object_vars($this);
        array_walk($vars, function ($value, $key) use (&$data) {
            if (!is_null($value) && $key !== 'customProperties') {
                $key = '_' . strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $key));
                $data[$key] = $value;
            }
        });
        $data = array_merge($data, $this->customProperties);
        return $data;
    }
   
}